package com.example.helloword;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.helloword.ui.PagesInReadView;
import com.example.helloword.ui.SearchActivity;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    private Button button;
    String[] permissions = new String[]{
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        new PermissionManager(permissions).checkPermissions(this, 123);
//
//        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R ||
//                Environment.isExternalStorageManager()
//        ) {
//            Toast.makeText(this, "已获得访问所有文件权限", Toast.LENGTH_SHORT).show();
//        } else {
//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//                    builder.setMessage("本程序需要您同意允许访问所有文件权限");
//                    builder.setPositiveButton("确定",(DialogInterface dialogInterface, int i) ->{
//
//
//                    Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
//                startActivity(intent);
//
//            });
//            builder.show();
//        }


        LinearLayout viewById = findViewById(R.id.tab_bottom);
        ViewTreeObserver viewTreeObserver = viewById.getViewTreeObserver();
        TextView textView = new TextView(this);
        ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);



        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                viewById.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int measuredHeight = viewById.getMeasuredWidth();
                layoutParams.width=measuredHeight/4;
                textView.setLayoutParams(layoutParams);
                textView.setBackgroundColor(getResources().getColor(R.color.black));
                viewById.addView(textView);
                Log.v("MainActivity",measuredHeight+"");
            }
        });





        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SearchActivity.class);
                startActivity(intent);


            }
        });
//

    }
    private List<Address> getAddress(Location location){
        List<Address> result=null;
        if (location!=null){
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            try {
                result=geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Log.d("MainActivity","result:"+result.toString());
        Log.d("MainActivity","城市:"+result.get(0).getLocality());


        return result;
    }


    class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(@NonNull Location location) {
            Log.v("MyLocationListener","onLocationChanged");

        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
            Log.v("MyLocationListener","onStatusChanged");

        }


        @Override
        public void onProviderEnabled(@NonNull String provider) {
            LocationListener.super.onProviderEnabled(provider);
            Log.v("MyLocationListener","onProviderEnabled");

        }
    }



}